package com.gzhryc.jdbc.models;

import java.lang.reflect.Field;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.sql.rowset.serial.SerialBlob;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.jdbc.annotaion.Column;

public class Parameters {

	/** 查询的条件参数 */
	volatile List<Parameter> params;
	
	public Parameters(){
		this.params = new CopyOnWriteArrayList<Parameter>();
	}
	
	/**
	 * 设置一个查询参数
	 * @param obj
	 */
	public <T> void setObject(T obj){
		params.add(new Parameter(obj.getClass(),obj));
	}
	
	/**
	 * 设置一个Time类型的查询参数
	 * @param obj
	 */
	public void setTime(Object obj){
		params.add(new Parameter(Time.class,obj));
	}
	
	/**
	 * 设置一个Timestamp类型的查询参数
	 * @param obj
	 */
	public void setTimestamp(Object obj){
		params.add(new Parameter(Timestamp.class,obj));
	}
	
	/**
	 * 根据字段类型设置一个查询参数
	 * @param field
	 * @param obj
	 */
	public void setField(Field field,Object obj){
		if(Date.class.equals(field.getType())){
			Column  column = field.getAnnotation(Column.class);
			if(column != null && StringUtils.isNotBlank(column.type())){
				if("TIMESTAMP".equals(column.type().toUpperCase())){
					setTimestamp(obj);
					return;
				}else if("TIME".equals(column.type().toUpperCase())){
					setTime(obj);
					return;
				}else if("MEDIUMBLOB".equals(column.type().toUpperCase()) 
						|| "BLOB".equals(column.type().toUpperCase())) {
					params.add(new Parameter(byte.class,obj));
				}
			}
		}
		params.add(new Parameter(field.getType(),obj));
	}
	
	/**
	 * 清空查询条件参数
	 */
	public void clear(){
		this.params.clear();
	}
	
	/**
	 * 将查询参数设置到PreparedStatement对象中
	 * @param pstmt
	 * @throws SQLException 
	 */
	public void toPreparedStatement(PreparedStatement pstmt) throws SQLException{
		int index = 0;
		for(Parameter param : params){
			index++;
			if(Timestamp.class.equals(param.getType())){
				Date date = (Date)param.getObj();
				pstmt.setTimestamp(index, new Timestamp(date.getTime()));
			}else if(Time.class.equals(param.getType())){
				Date date = (Date)param.getObj();
				pstmt.setTime(index, new Time(date.getTime()));
			}else if(byte.class.equals(param.getType())){
				Blob blob = new SerialBlob((byte[])param.getObj());	//创建Blob对象
				pstmt.setBlob(index, blob);
			}else {
				pstmt.setObject(index, param.getObj());
			}
		}
	}

	public String toString(){
		StringBuffer stringBuffer = new StringBuffer("");
		for(Parameter param : params){
			stringBuffer.append("," + param.getObj());
		}
		if(stringBuffer.length() > 0){
			return stringBuffer.substring(1);
		}
		return stringBuffer.toString();
	}
	
	/**
	 * 参数对象
	 * @author 张浩
	 * @version 1.0
	 */
	protected static class Parameter {
		
		private final Object obj;
		private final Class<?> type;

		public Parameter(Class<?> type,Object obj){
			this.type = type;
			this.obj = obj;
		}

		public Object getObj() {
			return obj;
		}

		public Class<?> getType() {
			return type;
		}
	} 
}
